import httpx
from mcp.server import FastMCP

# # 初始化 FastMCP 服务器
app = FastMCP("web-search")


@app.tool()
async def web_search(query: str) -> str:
    """
    搜索互联网内容

    Args:
        query: 要搜索内容

    Returns:
        搜索结果的总结
    """

    async with httpx.AsyncClient() as client:
        response = await client.post(
            "https://open.bigmodel.cn/api/paas/v4/tools",
            headers={
                "Authorization": "ede8671f4a4047dcbd54c7fb3b9a246e.eJvnMVrJtqwGEbdF"
            },
            json={
                "tool": "web-search-pro",
                "messages": [{"role": "user", "content": query}],
                "stream": False,
            },
        )
        print(response.json())
        res_data = []
        for choice in response.json()["choices"]:
            for message in choice["message"]["tool_calls"]:
                search_results = message.get("search_result")
                if not search_results:
                    continue
                for result in search_results:
                    res_data.append(result["content"])

        return "\n\n\n".join(res_data)


if __name__ == "__main__":
    app.run(transport="stdio")
